define(function(require, exports) {
	var pubjs = require('pubjs');
	var util = require('util');
	var view  = require('@base/view');

	// 计划编辑，上限设定模块
	var Base = view.container.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				view_model: {
					data: [
						{value: '0', field: 'Budget', text: LANG('预算'),unit: LANG('元'), validRegex: /[^0-9\s]/},
						{value: '0', field: 'Impression', text: LANG('展示'),unit: LANG('千次'), validRegex: /[^0-9\s]/},
						{value: '0', field: 'Click', text: LANG('点击'),unit: LANG('次'), validRegex: /[^0-9\s]/}
					]
				}
			});

			this.Super('init', arguments);
		}
		,afterBuild: function() {
			this.append(
				`
					<span ms-repeat-item="data" class="pr15">
                        <label class="pr5">{{item.text}}</label>
                        <input type="text" style="width: 100px;" class="pr5" ms-duplex="item.value">
                        {{item.unit}}
					</span>
				`
			);
		}
		,reset: function() {
			this.vm.reset();
			return this;
		}
		,validate: function(data)
		{
			var valid = true;
			var c = this.vm.$.data;
			for(var i = 0; i < data.length; i++)
			{
				if(c[i].validRegex && data[i].value.match(c[i].validRegex))
				{
					pubjs.alert(LANG('%1 格式不符合,只支持整数', c[i].text));
					return false;
				}
			}

			return valid;
		}
		,getData: function(isObject) {
			var data = this.vm.$.data;
			var ret = isObject ? {} : [];
			if(data && data.length)
			{
				util.each(data, function(item) {
					if(isObject)
					{
						ret[item.field] = item.value;
					}
					else
					{
						ret.push({value: item.value, field: item.field});
					}
				})
			}

			return ret;
		}
		,setData: function(data) {
			var arr = this.vm.$.data;
			util.each(arr, function(item) {
				item.value = String(Math.floor(data[item.field] || 0));
			});

			return this;
		}
	});

	exports.base = Base;

	exports.batchEdit = view.container.extend({
		init: function(config) {
			config = pubjs.conf(config, {
			});
			this.Super('init', arguments);
		}
		,afterBuild: function()
		{
			this.append(`
				<div class="M-formConGroup">
					<label class="group-label">${LANG('总预算')}:</label>
					<div class="group-content mtn5">
						<span  pub-name="totalLimit" pub-mod="plan/modules/limit.base"/>
					</div>
				</div>
				<div class="M-formConGroup">
					<label class="group-label">${LANG('每日预算')}:</label>
					<div class="group-content mtn5">
						<span  pub-name="dailyLimit" pub-mod="plan/modules/limit.base"/>
					</div>
				</div>
			`);

			this.buildFromTemplate({});
		}
		,afterBuildTemplate: function()
		{
			// 如果有数据为赋值，则异步创建完后再将数据setData
			if (this.$afterCreateData)
			{
				this.setValue(this.$afterCreateData);
				this.$afterCreateData = null;
			}
		}
		,getValue: function() {
			var cs = this.$;
			var data = {};
			data.TotalLimit = cs.totalLimit.getData();
			data.DailyLimit = cs.dailyLimit.getData();
			return data;
		}
		,setValue: function(data) {
			if (data)
			{
				// 如果模块已经创建，则直接setData
				if (this.$ && this.$.totalLimit && this.$.dailyLimit)
				{
					this.$.totalLimit.setData(data.TotalLimit);
					this.$.dailyLimit.setData(data.DailyLimit);
				}
				// 模块还没创建，先保留数据，异步创建完后再将数据setData
				else
				{
					this.$afterCreateData = data;
				}
			}
			return this;
		}
	});

	exports.planBudget = Base.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'class': 'P-setPromoteGroup'
			});

			this.Super('init', arguments);
		},
		afterBuild: function(){
			this.append(
				`
					<span ms-repeat-item="data" class="pr15 P-setPromoteGroupBudget">
                        <label class="pr5">{{item.text}}</label>
                        <input type="text" style="width: 100px;" class="pr5" ms-duplex="item.value">
                        {{item.unit}}
					</span>
				`
			);
		}
	});
});